package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    //购物车，里面的数据是菜，根据用户id判断是哪个用户的购物车中的菜

    /**
     * 添加到购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //设置用户id，指定当前是哪个用户的购物车
        long uId = BaseContext.get();
        shoppingCart.setUserId(uId);

        //查找该用户的购物车
        LambdaQueryWrapper<ShoppingCart> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,uId);

        //查询添加的菜品或套餐是否在购物车内
        //菜品添加
        Long dishId = shoppingCart.getDishId();
        if(dishId!=null){
            wrapper.eq(ShoppingCart::getDishId,dishId);
        }
        //套餐添加
        else{
            wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //select * from shopping_cart where user_id = ? and dish_id = ?
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        //在购物车内
        if(one!=null){
            Integer number = one.getNumber();
            one.setNumber(number+1);
            shoppingCartService.updateById(one);
        }
        //不在购物车内
        else{
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            one=shoppingCart;
        }

        return R.success(one);
    }

    /**
     * 查看当前用户的购物车的菜
     * @return
     */

    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        long uId = BaseContext.get();
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,uId);
        queryWrapper.orderByDesc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list= shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 清空购物车里的菜
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        long uId = BaseContext.get();
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,uId);
        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }

    /**
     * 删除购物车里的菜
     */
    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){

        long uId = BaseContext.get();
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,uId);
        if(shoppingCart.getDishId()!=null){
            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }
        else{
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        if(one!=null){
            if(one.getNumber()>1){
                Integer number = one.getNumber();
                one.setNumber(number-1);
                shoppingCartService.updateById(one);
            }
            else{
                shoppingCartService.removeById(one);
            }
        }
        return R.success("删除购物车成功");
    }
}
